package com.ym.utils.restful;

import com.ym.utils.exception.CustomException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Map;

/**
 * Ajax返回信息实体
 *
 * @author Whw
 * @version V1.0.00
 */
@Data
@Accessors(chain = true)
@AllArgsConstructor
@ApiModel(value="返回封装参数对象", description="描述：返回封装体")
public class AjaxJson implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    @ApiModelProperty(value = "状态码")
    private Integer status = 200;
    /**
     * 操作是否成功
     */
    @ApiModelProperty(value = "操作是否成功")
    private boolean success = true;
    /**
     * 提示信息
     */
    @ApiModelProperty(value = "提示信息")
    private String msg = "操作成功";
    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据")
    private Object data = null;

    private Object obj =null;

    /**
     * 返回其他参数
     */
    @ApiModelProperty(value = "返回其他参数")
    private Map<String, Object> attributes;

    public AjaxJson() {

    }

    // 成功但是无参数
    public AjaxJson Success(){
        return new AjaxJson(200,true,"操作成功",null,null,null);
    }

    // 成功有参数
    public AjaxJson Success(Object data){
        return new AjaxJson(200,true,"操作成功",data,null,null);
    }

    public AjaxJson error(int code, String msg) {
        return new AjaxJson(code,false,msg,null,null,null);
    }
    // 出现业务异常
    public AjaxJson ErrorException(CustomException e){
        if(e.getCode()==null){
            return new AjaxJson(500,false,"操作失败",null,null,null);
        }else{
            return new AjaxJson(e.getCode(),false,e.getMessage(),null,null,null);
        }
    }
}